package com.taijihuabao.modules.controller;

/**
 * @title: PermissionController
 * @Author luoxianan
 * @Date: 2021/11/29 11:56
 * @Version 1.0
 */

import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.common.jsonresult.JsonResult;
import com.taijihuabao.modules.entity.PermissionEntity;
import com.taijihuabao.modules.entity.RoleEntity;
import com.taijihuabao.modules.entity.UserEntity;
import com.taijihuabao.modules.model.basedto.IdDTO;
import com.taijihuabao.modules.model.permission.dto.PermissionCrFromDTO;
import com.taijihuabao.modules.model.permission.dto.PermissionListFromDTO;
import com.taijihuabao.modules.model.permission.dto.PermissionUpFromDTO;
import com.taijihuabao.modules.model.permission.vo.PermissionInfoVO;
import com.taijihuabao.modules.model.user.vo.UserInfoVO;
import com.taijihuabao.modules.service.PermissionService;
import com.taijihuabao.modules.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理
 * @author Wang
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/api/v1/permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;
    @Autowired
    UserService userService;

    /**
     * 权限列表
     *
     * @param listFromDTO
     * @return
     */
    @ApiOperation("权限列表")
    @GetMapping
    public JsonResult selectAllPermissions(PermissionListFromDTO listFromDTO){
        PageInfo pageInfo = permissionService.selectAllPermissions(listFromDTO);
        return JsonResult.success(pageInfo);
    }

    /**
     * 获取所有权限
     *
     * @return
     */
    @ApiOperation("所有权限")
    @GetMapping("/selectAll")
    public JsonResult selectAll() {
        List<PermissionEntity> data = permissionService.selectAll();
        return JsonResult.success(data);
    }

    /**
     * 通过id获取权限
     *
     * @param idDTO
     * @return
     */
    @ApiOperation("权限信息")
    @GetMapping("/info")
    public JsonResult selectById(IdDTO idDTO){
        PermissionInfoVO data = permissionService.selectById(idDTO.getId());
        return JsonResult.success(data);
    }

    /**
     * 新增权限
     *
     * @param crFromDTO
     * @return
     */
    @ApiOperation("新增权限")
    @PostMapping("/addPermission")
    public JsonResult insertPermission(@RequestBody PermissionCrFromDTO crFromDTO){
        permissionService.insertPermission(crFromDTO);
        return JsonResult.success();
    }

    /**
     * 编辑权限
     *
     * @param upFromDTO
     * @return
     */
    @ApiOperation("编辑权限")
    @PutMapping("/updatePermission")
    public JsonResult updatePermission(@RequestBody PermissionUpFromDTO upFromDTO){
        permissionService.updatePermission(upFromDTO);
        return JsonResult.success();
    }

    @ApiOperation("恢复已删除权限")
    @PutMapping("/updateDelPermission")
    public JsonResult updateDelPermission(@RequestBody IdDTO idDTO){
        permissionService.updateDelPermission(idDTO.getId());
        return JsonResult.success();
    }

    /**
     * 删除权限
     *
     * @param idDTO
     * @return
     */
    @ApiOperation("删除权限")
    @DeleteMapping("/deletePermission")
    public JsonResult deletePermission(@RequestBody IdDTO idDTO){
        permissionService.deletePermission(idDTO.getId());
        return JsonResult.success();
    }

    /**
     * 判断用户是否有权限对权限增删改
     *
     * @param loginUser
     */
    private void userPermission(UserEntity loginUser){
        UserInfoVO userInfoVO = userService.selectById(loginUser.getId());
        List<RoleEntity> roles = userInfoVO.getRoles();
        boolean flag = false;
        for (RoleEntity role : roles) {
            if ("管理员".equals(role.getName())){
                flag = true;
                break;
            }
        }
        if (!flag){
            throw new ServiceException(ApplicationResponseCode.USER_OUT_PERMISSION);
        }
    }
}
